<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JavaScript</title>
</head>

<body>
  <p>构造函数</p>
  <script>
    function Person(name, age) {
      this.name = name
      this.age = age
      this.sayName = function (){
        console.log('我是',this.name)
      }
      this.sayAge = sayAge
    }
    // (优点)在全局作用于定义sayAge函数不在Person中定义可以防止每个实例都有自己的sayAge优化了性能
    // （缺点）但是污染了全局作用域而且定义在全局作用域中不安全
    function sayAge(){
      console.log('我',this.age)
    }
    var p1 = new Person('张三', 16)
    console.log(p1)
    p1.sayName()
    p1.sayAge()
  </script>
</body>

</html>